package com.dhcc.cdc.sys.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.service.ICdcDataService;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.result.node.ZTreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 院区数据、科室数据、患者数据、检验数据等表数据服务层接口
 * 
 * @ClassName: CdcDataServiceImpl
 * @Description: TODO
 * @author jumper
 * @date 2021-03-31 17:43:57
 */
@Service("cdcDataService")
public class CdcDataServiceImpl extends BaseServiceImpl<CdcDataMapper, CdcDataEntity> implements ICdcDataService {

    @Autowired
    IDictionaryService dictionarySrv;

    @Override
    public List<CdcDataEntity> page(Page<CdcDataEntity> page, Map<String, Object> map) {
        return this.baseMapper.page(page, map);
    }

    @Override
    public List<CdcDataEntity> list(Map<String, Object> map) {
        return this.baseMapper.list(map);
    }

    @Override
    public List<ZTreeNode> tree(String dataDesc, Long dataTypeId) {
        List<ZTreeNode> tree = this.baseMapper.tree(dataDesc, dataTypeId);
        tree.add(ZTreeNode.createRoot("数据中心"));
        return tree;
    }

    public List<ZTreeNode> tree(String dataDesc, String dataTypeCode) {
        List<ZTreeNode> tree = this.baseMapper.tree(dataDesc, null);
        if (dataTypeCode != null) {
            tree = filtByTypeCode(tree, dataTypeCode);
        }
        tree.add(ZTreeNode.createRoot("数据中心"));
        return tree;
    }

    @Override
    public List<ZTreeNode> treeAll(String dataDesc, String dataTypeCode) {
        List<ZTreeNode> tree = this.baseMapper.treeAll(dataDesc, null);
        if (dataTypeCode != null) {
            tree = filtByTypeCode(tree, dataTypeCode);
        }
        tree.add(ZTreeNode.createRoot("数据中心"));
        return tree;
    }

    @Override
    public CdcDataEntity selectByCode(String code) {
        return this.baseMapper.selectByCode(code);
    }

    public List<ZTreeNode> filtByTypeCode(List<ZTreeNode> tree, String dataTypeCode) {
        List<ZTreeNode> result = new ArrayList<ZTreeNode>();
        DictionaryEntity typeCache=null;
        Long typeIdCache=null;
        for (ZTreeNode node : tree) {
            Long dataId = node.getId();
            if (node.getIsParent()) {
                result.add(node);
                continue;
            }
            CdcDataEntity data = this.baseMapper.selectById(dataId);
            if (data == null)
                continue;
            Long typeId = data.getCdcDataTypeId();
            if (typeId != null) {
                DictionaryEntity type =null;
                if(typeId.equals(typeIdCache)) {
                    type = typeCache;
                }else{
                    type = this.dictionarySrv.selectById(typeId);
                    typeCache=type;
                    typeIdCache=typeId;
                }
                String typeCode = type.getCode();
                if (typeCode.contains(dataTypeCode))
                    result.add(node);
            }
        }
        return result;
    }

}